﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using Common.Protocol;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
namespace PPTControl
{
    class MouseUtil
    {
        /// <summary>
        /// 鼠标控制参数
        /// </summary>
        const int MOUSEEVENTF_LEFTDOWN = 0x2;
        const int MOUSEEVENTF_LEFTUP = 0x4;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
        const int MOUSEEVENTF_MIDDLEUP = 0x40;
        const int MOUSEEVENTF_MOVE = 0x1;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        const int MOUSEEVENTF_RIGHTDOWN = 0x8;
        const int MOUSEEVENTF_RIGHTUP = 0x10;

        /// <summary>
        /// 鼠标的位置
        /// </summary>
        public struct PONITAPI
        {
            public int x, y;
        }

        [DllImport("user32.dll")]
        public static extern int GetCursorPos(ref PONITAPI p);

        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public static ControlResp Move(double offsetX, double offsetY)
        {
            //PONITAPI p = new PONITAPI();
            //GetCursorPos(ref p);
            //p.x += offsetX;
            //p.y += offsetY;
            //SetCursorPos(p.x, p.y);
            
            ControlResp ret = new ControlResp();
            return ret;
        }

        public static Point LogicToScreen(double x,double y)
        {
            Rectangle rect = Screen.PrimaryScreen.WorkingArea;
            
            Point result = new Point();
            result.X =(int)( x *rect.Width);
            result.Y =(int) (y *rect.Height);
            
            result.X = Math.Max(0, result.X);
            result.X = Math.Min(rect.Width, result.X);

            result.Y = Math.Max(0, result.Y);
            result.Y = Math.Min(rect.Height, result.Y);


            return result;
                
        }
        public static ControlResp Locate(int posX, int posY)
        {
            
            SetCursorPos(posX,posY);
            ControlResp ret = new ControlResp();
            return ret;
        }

        public static ControlResp Click()
        {
            PONITAPI p = new PONITAPI();
            GetCursorPos(ref p);
            mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
            ControlResp ret = new ControlResp();
            return ret;
        }

        public static ControlResp DoubleClick(int posX, int posY)
        {
            ControlResp ret = new ControlResp();
            return ret;
        }
    }
}
